package com.rfid.project.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;

import biz.util.*;

import com.rfid.project.model.*;
import com.rfid.project.dao.*;
import com.mysema.query.BooleanBuilder;

import java.util.List;

@Service
@Transactional
public class CommonMilestoneService {

	@Autowired
	private CommonMilestoneDao commonMilestoneDao;

	private QCommonMilestone $ = QCommonMilestone.commonMilestone;

	@Autowired
	public void setCommonMilestoneDao(CommonMilestoneDao commonMilestoneDao) {
		this.commonMilestoneDao = commonMilestoneDao;
	}

	public List<CommonMilestone> findAll() {
		return ListUtil.toList(commonMilestoneDao.findAll());
	}

	public List<CommonMilestone> findAll(String name, String remark,
			Long projectTypeId, String orderByAttribute) {
		BooleanBuilder condition = new BooleanBuilder();

		if (StringUtil.isNotEmpty(name)) {
			condition.and($.name.contains(name));
		}

		if (StringUtil.isNotEmpty(remark)) {
			condition.and($.remark.contains(remark));
		}

		if (IdUtil.isNotEmpty(projectTypeId)) {
			condition.and($.projectType.id.eq(projectTypeId));
		}

		return ListUtil.toList(commonMilestoneDao.findAll(condition));
	}

	public Page<CommonMilestone> findAll(String name, String remark,
			Long projectTypeId, int pageNumber, int pageSize,
			String sortAttribute, String orderDirection) {
		BooleanBuilder condition = new BooleanBuilder();

		if (StringUtil.isNotEmpty(name)) {
			condition.and($.name.contains(name));
		}
		if (StringUtil.isNotEmpty(remark)) {
			condition.and($.remark.contains(remark));
		}
		if (IdUtil.isNotEmpty(projectTypeId)) {
			condition.and($.projectType.id.eq(projectTypeId));
		}

		/*Sort sortObject = null;
		if (StringUtil.isEmpty(sortAttribute)) {
			sortObject = new Sort(Direction.DESC, "id");
		} else {
			sortObject = new Sort(Direction.fromStringOrNull(orderDirection),
					sortAttribute);
		}
		*/
		
		Sort sortObject_1 = new Sort(Direction.DESC, "orderNumber");
		PageRequest pageRequest = new PageRequest(pageNumber - 1, pageSize,
				sortObject_1);
		return commonMilestoneDao.findAll(condition, pageRequest);
	}

	public CommonMilestone getCommonMilestone(Long id) {
		return commonMilestoneDao.getOne(id);
	}

	public void deleteCommonMilestone(Long id) {
		commonMilestoneDao.delete(id);
	}

	public void saveCommonMilestone(CommonMilestone commonMilestone) {
		if (null == commonMilestone.getOrderNumber()) {
			commonMilestone.setOrderNumber(getOrderNumber());
		}
		commonMilestoneDao.save(commonMilestone);
	}

	private int getOrderNumber() {
		BooleanBuilder condition = new BooleanBuilder();
		Sort sortObject = new Sort(Direction.DESC, "orderNumber");
		PageRequest pageRequest = new PageRequest(0, 1, sortObject);
		Page<CommonMilestone> items = this.commonMilestoneDao.findAll(
				condition, pageRequest);
		if (items.getTotalElements() > 0) {
			CommonMilestone item = items.getContent().get(0);
			return item.getOrderNumber() + 1;
		}
		return 1;
	}
	
	
	public boolean changeOrderNumber(Long id, Long targetId){
		CommonMilestone currentModule = this.commonMilestoneDao.findOne(id);
		CommonMilestone targetModule = this.commonMilestoneDao.findOne(targetId);
		int orderNumber = currentModule.getOrderNumber();
		currentModule.setOrderNumber(targetModule.getOrderNumber());
		targetModule.setOrderNumber(orderNumber);
		
		this.commonMilestoneDao.save(currentModule);
		this.commonMilestoneDao.save(targetModule);
		return true;
	}

	public CommonMilestone findOne(Long id) {
		return this.commonMilestoneDao.findOne(id);
	}

}
